package net.caixiaomi.info.Lottery.presenter;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.Lottery.callback.BallCallBack;
import net.caixiaomi.info.Lottery.callback.LotteryMainCallBack;
import net.caixiaomi.info.Lottery.model.BallItem;
import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.Lottery.model.LotteryTicketInfo;
import net.caixiaomi.info.Lottery.ui.BallLotteryMainActivity;
import net.caixiaomi.info.Lottery.utils.RandomUtils;
import net.caixiaomi.info.Lottery.view.BallView;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * Created by suntian on 2018/8/8.
 */

public class BallPresenter implements BallCallBack {

    private Context mContext;
    private BallView ballViewBefore;
    private BallView ballViewAfter;

    private BallView ballViewDanBefore;
    private BallView ballViewTuoBefore;
    private BallView ballViewDanAfter;
    private BallView ballViewTuoAfter;
    private int mCurrentPosition;
    private int mStandardChips;
    private long mStandardMoney;
    private int mCourageChips;
    private long mCourageMoney;
    private int price=2; // 单注价格
    private int multi = 1; // 倍投
    private LotteryMainCallBack mCallBack;

    public BallPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public void initStandardView(BallView ballViewBefore, BallView ballViewAfter,int position, LotteryMainCallBack callback){
        this.ballViewBefore=ballViewBefore;
        this.ballViewAfter=ballViewAfter;
        mCallBack=callback;
        mCurrentPosition=position;
        loadData();
        initData();
    }

    public void initCourageView(BallView ballViewDanBefore,BallView ballViewTuoBefore,BallView ballViewDanAfter,BallView ballViewTuoAfter, int position,LotteryMainCallBack callback){
        this.ballViewDanBefore=ballViewDanBefore;
        this.ballViewTuoBefore=ballViewTuoBefore;
        this.ballViewDanAfter=ballViewDanAfter;
        this.ballViewTuoAfter=ballViewTuoAfter;
        mCallBack=callback;
        mCurrentPosition=position;
        loadData();
        initData();
    }

    public void setShock(){

        List<BallItem> listBefore=new ArrayList();

        int befores[]= RandomUtils.getRandomNum(35,1,5);
        int afters[]=RandomUtils.getRandomNum(12,1,2);

        for(int i=0;i<befores.length;i++){
            BallItem ballItem=new BallItem();
            ballItem.setSelect(true);
            ballItem.setNum(befores[i]);
            listBefore.add(ballItem);
        }
        List<BallItem> listAfter=new ArrayList();
        for(int i=0;i<afters.length;i++){
            BallItem ballItem=new BallItem();
            ballItem.setSelect(true);
            ballItem.setNum(afters[i]);
            listAfter.add(ballItem);
        }
        ballViewBefore.setCheckData(listBefore,true);
        ballViewAfter.setCheckData(listAfter,true);
    }

    private void setInfo(LotteryTicketInfo info){
        List<BallItem> preList=new ArrayList<>();
        List<BallItem> postList=new ArrayList<>();
        for(int i=0;i<35;i++){
            BallItem ball=new BallItem();
            ball.setNum(i+1);
            ball.setMissNum(info.getPreList().get(i));
            preList.add(ball);
        }
        for(int i=0;i<12;i++){
            BallItem ball=new BallItem();
            ball.setNum(i+1);
            ball.setMissNum(info.getPostList().get(i));
            postList.add(ball);
        }
        if(mCurrentPosition==0) {
            ballViewBefore.setMissNum(preList);
            ballViewAfter.setMissNum(postList);
        }else{
            ballViewDanBefore.setMissNum(preList);
            ballViewTuoBefore.setMissNum(preList);
            ballViewDanAfter.setMissNum(preList);
            ballViewTuoAfter.setMissNum(preList);
        }
        mCallBack.getLottertInfo(info);
    }

    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("emptyStr", "");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<LotteryTicketInfo>> baseCallModelCall = requestService.getTicketInfo(params);
        baseCallModelCall.enqueue(new ResponseCallback<BaseCallModel<LotteryTicketInfo>>() {
            @Override
            public void onSuccess(BaseCallModel<LotteryTicketInfo> data) {
                setInfo(data.data);
            }

            @Override
            public void onFail(ResponseError error) {

            }

            @Override
            public void onError() {

            }
        });

    }


    @Override
    public boolean gonChangeBallData(int type,BallItem ball) {
        return onBallChanged(type,ball);
    }

    /*
    * 即时更新投注信息
    */
    public boolean onBallChanged(int type,BallItem ball){
        int statusTtotalChips = 0;
        int frontDanCount = 0; // 前区胆码数据
        int frontTuoCount = 0;
        int backDanCount = 0;
        int backTuoCount = 0;
        int frontChips = 0;
        int backChips = 0;
        if(mCurrentPosition==0){
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                if(ballViewBefore.getBallData().get(i).isSelect()){
                    frontTuoCount++;
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                if(ballViewAfter.getBallData().get(i).isSelect()){
                    backTuoCount++;
                }
            }
            if(frontTuoCount>18){
                showTimerToast("最多只能选择18个红球");
//                ToastUtil.s("最多只能选择18个红球");
                return false;
            }
        }else{
            for(int i=0;i<ballViewDanBefore.getBallData().size();i++){
                if(ballViewDanBefore.getBallData().get(i).isSelect()){
                    frontDanCount++;
                }
            }
            for(int i=0;i<ballViewTuoBefore.getBallData().size();i++){
                if(ballViewTuoBefore.getBallData().get(i).isSelect()){
                    frontTuoCount++;
                }
            }
            for(int i=0;i<ballViewDanAfter.getBallData().size();i++){
                if(ballViewDanAfter.getBallData().get(i).isSelect()){
                    backDanCount++;
                }
            }
            for(int i=0;i<ballViewTuoAfter.getBallData().size();i++){
                if(ballViewTuoAfter.getBallData().get(i).isSelect()){
                    backTuoCount++;
                }
            }
            if(frontDanCount>4){
//                ToastUtil.s("胆码红球至多选4个");
                return false;
            }
            //蓝胆只能选一个
            if(backDanCount>1){
                backDanCount--;
                for (int i = 0; i < ballViewDanAfter.getBallData().size(); i++) {
                    BallItem tarBall=ballViewDanAfter.getBallData().get(i);
                    if (tarBall.isSelect()&&tarBall.getNum() != ball.getNum()) {
                        BallItem resultBall=new BallItem();
                        resultBall.setNum(tarBall.getNum());
                        resultBall.setSelect(false);
                        List<BallItem>list=new ArrayList<>();
                        list.add(resultBall);
                        ballViewDanAfter.setCheckData(list,false);
//                        return true;
                    }
                }
//                ToastUtil.s("胆码蓝球至多选1个");
//                return false;
            }

            //--------如果担被选择，相同的号码拖要取消
            if(ball!=null&&ball.isSelect()) {
                switch (type) {
                    case BallLotteryMainActivity.BALL_BEFORE_DAN:
                        for (int i = 0; i < ballViewDanBefore.getBallData().size(); i++) {
                            BallItem tarBall=ballViewDanBefore.getBallData().get(i);
                            if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                                frontTuoCount--;
                                break;
                            }
                        }
                        break;
                    case BallLotteryMainActivity.BALL_BEFORE_TUO:
                        for (int i = 0; i < ballViewTuoBefore.getBallData().size(); i++) {
                            BallItem tarBall=ballViewTuoBefore.getBallData().get(i);
                            if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                                frontDanCount--;
                                break;
                            }
                        }
                        break;
                    case BallLotteryMainActivity.BALL_AFTER_DAN:
                        for (int i = 0; i < ballViewDanAfter.getBallData().size(); i++) {
                            BallItem tarBall=ballViewDanAfter.getBallData().get(i);
                            if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                                backTuoCount--;
                                break;
                            }
                        }
                        break;
                    case BallLotteryMainActivity.BALL_AFTER_TUO:
                        for (int i = 0; i < ballViewTuoAfter.getBallData().size(); i++) {
                            BallItem tarBall=ballViewTuoAfter.getBallData().get(i);
                            if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                                backDanCount--;
                                break;
                            }
                        }
                        break;
                }
            }
            //---------
        }

        if (((frontDanCount + frontTuoCount) == 5 && frontDanCount > 0)
                || ((backDanCount + backTuoCount) == 2 && backDanCount > 0)) {
            statusTtotalChips = 0;
        } else {
            switch (frontDanCount) {
                case 4:// 选4胆
                    frontChips = frontTuoCount;
                    break;
                case 3:// 选3胆
                    frontChips = frontTuoCount * (frontTuoCount - 1) / (1 * 2);
                    break;
                case 2:// 选2胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) / (1 * 2 * 3);
                    break;
                case 1:// 选1胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) * (frontTuoCount - 3)
                            / (1 * 2 * 3 * 4);
                    break;
                case 0:// 选0胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) * (frontTuoCount - 3)
                            * (frontTuoCount - 4) / (1 * 2 * 3 * 4 * 5);
                    break;
            }
            switch (backDanCount) {
                case 1:// 选1胆
                    backChips = backTuoCount;
                    break;
                case 0:// 选0胆
                    backChips = backTuoCount * (backTuoCount - 1) / (1 * 2);
                    break;
            }
            if (frontChips * backChips >= 1) {
                statusTtotalChips = frontChips * backChips;
            }
        }

        long tempBetMoney = (long) multi * statusTtotalChips * price;
        int tempTotalChips = statusTtotalChips;

        if(tempBetMoney>20000&&mCurrentPosition==0){
            showTimerToast("单次投注最多2万元");
//            ToastUtil.s("单次投注最多2万元");
            return false;
        }

        //--------如果担被选择，相同的号码拖要取消
        if(ball!=null&&ball.isSelect()) {
            switch (type) {
                case BallLotteryMainActivity.BALL_BEFORE_DAN:
                    for (int i = 0; i < ballViewDanBefore.getBallData().size(); i++) {
                        BallItem tarBall=ballViewDanBefore.getBallData().get(i);
                        if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                            BallItem resultBall=new BallItem();
                            resultBall.setNum(tarBall.getNum());
                            resultBall.setSelect(false);
                            List<BallItem>list=new ArrayList<>();
                            list.add(resultBall);
                            ballViewTuoBefore.setCheckData(list,false);
                            return true;
                        }
                    }
                    break;
                case BallLotteryMainActivity.BALL_BEFORE_TUO:
                    for (int i = 0; i < ballViewTuoBefore.getBallData().size(); i++) {
                        BallItem tarBall=ballViewTuoBefore.getBallData().get(i);
                        if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                            BallItem resultBall=new BallItem();
                            resultBall.setNum(tarBall.getNum());
                            resultBall.setSelect(false);
                            List<BallItem>list=new ArrayList<>();
                            list.add(resultBall);
                            ballViewDanBefore.setCheckData(list,false);
                            return true;
                        }
                    }
                    break;
                case BallLotteryMainActivity.BALL_AFTER_DAN:
                    for (int i = 0; i < ballViewDanAfter.getBallData().size(); i++) {
                        BallItem tarBall=ballViewDanAfter.getBallData().get(i);
                        if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                            BallItem resultBall=new BallItem();
                            resultBall.setNum(tarBall.getNum());
                            resultBall.setSelect(false);
                            List<BallItem>list=new ArrayList<>();
                            list.add(resultBall);
                            ballViewTuoAfter.setCheckData(list,false);
                            return true;
                        }
                    }
                    break;
                case BallLotteryMainActivity.BALL_AFTER_TUO:
                    for (int i = 0; i < ballViewTuoAfter.getBallData().size(); i++) {
                        BallItem tarBall=ballViewTuoAfter.getBallData().get(i);
                        if (tarBall.isSelect()&&tarBall.getNum() == ball.getNum()) {
                            BallItem resultBall=new BallItem();
                            resultBall.setNum(tarBall.getNum());
                            resultBall.setSelect(false);
                            List<BallItem>list=new ArrayList<>();
                            list.add(resultBall);
                            ballViewDanAfter.setCheckData(list,false);
                            return true;
                        }
                    }
                    break;
            }
        }

        String des="";
        boolean correct;
        if(statusTtotalChips==0){
            if(mCurrentPosition==0){
                des="<html><body>请至少选择<font color='red'>5</font>个红球 <font color='blue'>2</font>个蓝球 </body></html>";
            }else{
                des="<html><body>请至少选择<font color='red'>6</font>个红球 <font color='blue'>2</font>个蓝球 </body></html>";
            }
            correct=false;
        }else{
//            des="共"+tempTotalChips+"注  合计"+tempBetMoney+"元";
            des="<html><body>共<font color='red'>"+tempTotalChips+"</font>注  合计<font color='red'>"+tempBetMoney+"</font>元</body></html>";
            correct=true;
        }
        mCallBack.getkTotalDes(des,correct);

        if(mCurrentPosition==0){
            mStandardChips=tempTotalChips;
            mStandardMoney=tempBetMoney;
        }else{
            mCourageChips=tempTotalChips;
            mCourageMoney=tempBetMoney;
        }

        return true;
    }


    long firstTime=0;
    /*
        3秒内点击认为是一次
     */
    private void showTimerToast(String msg){
        long secondTime= System.currentTimeMillis();
        if(secondTime-firstTime>3000){
            firstTime=secondTime;
            ToastUtil.s(msg);
        }
    }

    private LotteryBettingEntity getData(){
        LotteryBettingEntity entity=new LotteryBettingEntity();
        if(mCurrentPosition==0){
            List<String> befores=new ArrayList<>();
            List<String> afters=new ArrayList<>();
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                BallItem item=ballViewBefore.getBallData().get(i);
                if(item.isSelect()){
                    befores.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                BallItem item=ballViewAfter.getBallData().get(i);
                if(item.isSelect()){
                    afters.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            entity.setmBefores(befores);
            entity.setmAfters(afters);
            if(befores.size()==5&&afters.size()==2){
                entity.setStatus(0);
            }else{
                entity.setStatus(1);
            }
            entity.setChips(mStandardChips);
            entity.setMoney(mStandardMoney);
        }else{
            List<String> beforeDans=new ArrayList<>();
            List<String> afterDans=new ArrayList<>();
            List<String> beforeTuos=new ArrayList<>();
            List<String> afterTuos=new ArrayList<>();
            for(int i=0;i<ballViewDanBefore.getBallData().size();i++){
                BallItem item=ballViewDanBefore.getBallData().get(i);
                if(item.isSelect()){
                    beforeDans.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            for(int i=0;i<ballViewTuoBefore.getBallData().size();i++){
                BallItem item=ballViewTuoBefore.getBallData().get(i);
                if(item.isSelect()){
                    beforeTuos.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            for(int i=0;i<ballViewDanAfter.getBallData().size();i++){
                BallItem item=ballViewDanAfter.getBallData().get(i);
                if(item.isSelect()){
                    afterDans.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            for(int i=0;i<ballViewTuoAfter.getBallData().size();i++){
                BallItem item=ballViewTuoAfter.getBallData().get(i);
                if(item.isSelect()){
                    afterTuos.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            entity.setStatus(2);
            entity.setmBeforeDans(beforeDans);
            entity.setmBeforeTuos(beforeTuos);
            entity.setmAfterDans(afterDans);
            entity.setmAfterTuos(afterTuos);
            entity.setChips(mCourageChips);
            entity.setMoney(mCourageMoney);
        }
        return entity;
    }

    public boolean addBettingData(){
        if(mCurrentPosition==1&&mCourageMoney>20000){
//            ToastUtil.s("单次投注最多2万元");
            showTimerToast("单次投注最多2万元");
            return false;
        }

        LotteryBettingEntity entity=getData();
        if(CommonApp.mLotteryIndex!=-1){
            CommonApp.mLotteryBettingList.remove(CommonApp.mLotteryIndex);
            CommonApp.mLotteryBettingList.add(CommonApp.mLotteryIndex,entity);
        }else{
            CommonApp.mLotteryBettingList.add(entity);
        }
        return true;
    }

    private void initData(){
        if(CommonApp.mLotteryIndex!=-1){
//            if(CommonApp.mLotteryBettingList.get(CommonApp.mLotteryIndex).getStatus()==mCurrentPosition){
                LotteryBettingEntity entity=CommonApp.mLotteryBettingList.get(CommonApp.mLotteryIndex);
                if(mCurrentPosition==0){
                    if(entity.getStatus()==2){
                        return;
                    }
                    List<BallItem> listBefore=new ArrayList();
                    for(int i=0;i<entity.getmBefores().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmBefores().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmBefores().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmBefores().get(i)));
                        }
                        listBefore.add(ballItem);
                    }
                    List<BallItem> listAfter=new ArrayList();
                    for(int i=0;i<entity.getmAfters().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmAfters().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmAfters().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmAfters().get(i)));
                        }
                        listAfter.add(ballItem);
                    }
                    ballViewBefore.setCheckData(listBefore,true);
                    ballViewAfter.setCheckData(listAfter,true);
                }else{
                    if(entity.getStatus()!=2){
                        return;
                    }
                    List<BallItem> listBeforeDan=new ArrayList();
                    for(int i=0;i<entity.getmBeforeDans().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmBeforeDans().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmBeforeDans().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmBeforeDans().get(i)));
                        }
                        listBeforeDan.add(ballItem);
                    }
                    List<BallItem> listBeforeTuo=new ArrayList();
                    for(int i=0;i<entity.getmBeforeTuos().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmBeforeTuos().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmBeforeTuos().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmBeforeTuos().get(i)));
                        }
                        listBeforeTuo.add(ballItem);
                    }
                    List<BallItem> listAfterDans=new ArrayList();
                    for(int i=0;i<entity.getmAfterDans().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmAfterDans().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmAfterDans().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmAfterDans().get(i)));
                        }
                        listAfterDans.add(ballItem);
                    }
                    List<BallItem> listAfterTuos=new ArrayList();
                    for(int i=0;i<entity.getmAfterTuos().size();i++){
                        BallItem ballItem=new BallItem();
                        ballItem.setSelect(true);
                        String target=entity.getmAfterTuos().get(i).substring(0,1);
                        if(target.equals("0")){
                            ballItem.setNum(Integer.parseInt(entity.getmAfterTuos().get(i).substring(1)));
                        }else{
                            ballItem.setNum(Integer.parseInt(entity.getmAfterTuos().get(i)));
                        }
                        listAfterTuos.add(ballItem);
                    }
                    ballViewDanBefore.setCheckData(listBeforeDan,true);
                    ballViewTuoBefore.setCheckData(listBeforeTuo,true);
                    ballViewDanAfter.setCheckData(listAfterDans,true);
                    ballViewTuoAfter.setCheckData(listAfterTuos,true);
                }
            }
        }


    public boolean isNoBallCheck(){
        if(mCurrentPosition==0){
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                if(ballViewBefore.getBallData().get(i).isSelect()){
                    return false;
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                if(ballViewAfter.getBallData().get(i).isSelect()){
                    return false;
                }
            }
        }else{
            for(int i=0;i<ballViewDanBefore.getBallData().size();i++){
                if(ballViewDanBefore.getBallData().get(i).isSelect()){
                    return false;
                }
            }
            for(int i=0;i<ballViewTuoBefore.getBallData().size();i++){
                if(ballViewTuoBefore.getBallData().get(i).isSelect()){
                    return false;
                }
            }
            for(int i=0;i<ballViewDanAfter.getBallData().size();i++){
                if(ballViewDanAfter.getBallData().get(i).isSelect()){
                    return false;
                }
            }
            for(int i=0;i<ballViewTuoAfter.getBallData().size();i++){
                if(ballViewTuoAfter.getBallData().get(i).isSelect()){
                    return false;
                }
            }
        }
        return true;
    }
}
